package practice.hot100;

import java.util.HashMap;

public class Solution581 {
    public int findUnsortedSubarray(int[] nums) {
        int max = nums[0];
        int min = nums[nums.length - 1];
        int right = 0;
        int left = nums.length - 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] >= max) {
                max = nums[i];
            } else right = i;
            if (nums[nums.length - 1 - i] <= min) {
                min = nums[nums.length - i - 1];
            } else left = nums.length - 1 - i;
        }
        return right - left + 1;
    }
}
